# -*- coding: utf-8 -*-
"""
Created on Sat Jul 18 18:57:24 2021

@author: chibi
"""
import serial
import sys,os

run1 =  True

def dothings(temp):
    global ser 
    print("你输入的指令：%s" % temp)
    temp = temp.encode("utf-8")
    ser.write(temp)
    
    while run1:
       line = ser.readline()
       #ser.read(10)
       print(line)
       line = line.decode('utf-8').strip() #去掉头尾的换行
       
       if  len(line) > 0:
            if line == "1010":
                print("led1 关")
            elif line == "1011":
                print("led1 自动开")
            elif line == "1111":
                print("led1 常开")
            elif line == "1020":
                print("led2 关")
            elif line == "1021":
                 print("led2 自动开")
            elif line == "1121":
                print("led2 常开")
            else :
                print("指令不在范围内")
       
       #print(line)
       break

with serial.Serial() as ser:
    ser.baudrate = 115200
    ser.port = 'COM7' 
    ser.timeout = 5
    ser.open()
    while True:
        inputdata =  input("输入指令：")
        #print("你输入的指令：%s" % inputdata)
        if  len(inputdata) > 0:
            if inputdata == "1":
                dothings("0011")
            elif inputdata == "2":
                dothings("0010")
            elif inputdata == "3":
                dothings("0111")
            elif inputdata == "4":
                dothings("0021")
            elif inputdata == "5":
                dothings("0020")
            elif inputdata == "6":
                dothings("0121")
            else :
                print("指令不在范围内")
            


"""
    while True:
        line = ser.readline()
        #ser.read(10)
        print(line)
        print(line.decode('utf-8'))
        #delay(10)
		
        #ser.write(b'hello')
"""